using System;
using System.Collections.Generic;
using UnityEngine;

public class AchievementManager : MonoBehaviour
{
	[Serializable]
	public class EditorReferences
	{
		public List<Achievement> achievements;
	}

	public static AchievementManager Instance;

	[SerializeField]
	public EditorReferences references = new EditorReferences();

	private Dictionary<string, List<Achievement>> _eventDictionary;

	private Dictionary<string, int> _temporaryDataStore = new Dictionary<string, int>();

	public Dictionary<string, int> TemporaryDataStore => _temporaryDataStore;

	private void Awake()
	{
		Instance = this;
		if (_eventDictionary == null)
		{
			_eventDictionary = new Dictionary<string, List<Achievement>>();
		}
	}

	public Achievement GetAchievementForSkin(int skinId)
	{
		return references.achievements.Find((Achievement a) => a.unlockableSkinId == skinId);
	}

	public void StartListening(string key, Achievement listener)
	{
		List<Achievement> value = null;
		if (_eventDictionary.TryGetValue(key, out value))
		{
			value.Add(listener);
			return;
		}
		value = new List<Achievement>();
		value.Add(listener);
		_eventDictionary.Add(key, value);
	}

	public void StopListening(string key, Achievement listener)
	{
		if (key != null)
		{
			List<Achievement> value = null;
			if (_eventDictionary.TryGetValue(key, out value))
			{
				value.Remove(listener);
			}
		}
	}

	public void TriggerEvent(string key)
	{
		List<Achievement> value = null;
		if (_eventDictionary.TryGetValue(key, out value))
		{
			foreach (Achievement item in value)
			{
				item.CheckValidation();
			}
		}
	}

	public int GetData(AchievementKeys key)
	{
		int value = 0;
		if (_temporaryDataStore.TryGetValue(key.ToString(), out value))
		{
			return value;
		}
		return PlayerPrefs.GetInt(key.ToString(), 0);
	}

	public void SetTemporaryData(AchievementKeys key, int newData)
	{
		_temporaryDataStore[key.ToString()] = newData;
		TriggerEvent(key.ToString());
	}

	public void IncrementTemporaryData(AchievementKeys key, int increment)
	{
		int value = 0;
		value = ((!_temporaryDataStore.TryGetValue(key.ToString(), out value)) ? increment : (value + increment));
		_temporaryDataStore[key.ToString()] = value;
		TriggerEvent(key.ToString());
	}

	public void DeleteTemporaryData(AchievementKeys key)
	{
		int num = 0;
		if (_temporaryDataStore.ContainsKey(key.ToString()))
		{
			_temporaryDataStore.Remove(key.ToString());
		}
	}

	public void ResetTemporaryData()
	{
		_temporaryDataStore.Clear();
	}

	public void SetPersistentData(AchievementKeys key, int newData)
	{
		PlayerPrefs.SetInt(key.ToString(), newData);
		TriggerEvent(key.ToString());
	}

	public void IncrementPersistentData(AchievementKeys key, int increment)
	{
		int @int = PlayerPrefs.GetInt(key.ToString(), 0);
		PlayerPrefs.SetInt(key.ToString(), @int + increment);
		TriggerEvent(key.ToString());
	}

	public void DeletePersistentData(AchievementKeys key)
	{
		PlayerPrefs.DeleteKey(key.ToString());
	}
}
